package com.aofeng.hybridwork.android.task;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;

import com.aofeng.hybrid.activity.HybridActivity;
import com.aofeng.hybrid.service.EntityService;
import com.aofeng.hybrid.service.SqlService;
import com.aofeng.hybridwork.application.HybridApplication;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * 保存已抄表未缴费数据
 * Created by LGY on 2016-08-25.
 */
public class SavetTablebase extends AsyncTask<String, String, Boolean> {
    HybridActivity hybridActivity;
    Boolean param;
    JSONObject js;


    public SavetTablebase(HybridActivity hybridActivity) {
        this.hybridActivity = hybridActivity;
    }

    @Override
    /**
     * params:
     * 保存抄表数据
     */
    protected Boolean doInBackground(String... params) {
        try {
            param = new Boolean(params[1]);
            js = new JSONObject(params[0]);
            JSONObject jo = new JSONObject();
            SqlService ss = new SqlService(hybridActivity);
            //"',f_meter_path = '" + js.getString("f_meter_path") +
            String sql = "update t_handplan set f_tablebase = '" + js.getInt("f_tablebase")   +
                    "',f_mark = '" + js.getString("f_mark") +
                    "',f_reverse_state = '" + js.getInt("f_reverse_state") +
                    "',f_oughtamount = '" + js.getInt("f_oughtamount") +
                    "',f_meter_state = '已抄表' where f_userinfo_id =  '" + js.getInt("f_userinfo_id") +
                    "' and f_savehangid= '" + js.getInt("f_user_id") + "'";
            ss.run(sql);
            return true;
        } catch(Exception e) {
            return false;
        }
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        hybridActivity.createProgressDialog(ProgressDialog.STYLE_SPINNER);
    }

    @Override
    protected void onPostExecute(Boolean flag) {
        super.onPostExecute(flag);
        hybridActivity.closeProgress();
        hybridActivity.mWebView.loadUrl("javascript:HostApp.__callback__(" +  flag + ","+ param +");");
    }

    @Override
    protected void onProgressUpdate(String... values) {
        super.onProgressUpdate(values);
        int progress = Integer.parseInt(values[0]);
        hybridActivity.setProgress(progress, values[1]);
    }
}
